<?php

namespace App\Http\Controllers\admin;

use App\Column;
use App\Http\Requests\Article;
use App\Http\Requests\StatusFilter;
use App\Message;
use App\Post;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use mysql_xdevapi\Exception;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Pagination\LengthAwarePaginator;

class Article_mangement extends Controller
{
    //
    /**
     * 返回所有文章信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function getArticleInfo($id, StatusFilter $request)
    {
        $res = [];
        $data = [];
        $status = User::find(Auth::id());
        $perPage = 10;            //每页显示数量
        if ($request->has('page')) {
            $current_page = $request->input('page');
            $current_page = $current_page <= 0 ? 1 : $current_page;
        } else {
            $current_page = 1;
        }
        //$status=User::withTrashed()->find(4);//测试使用
        if ($status == null) {
            $res = array('code' => 100, 'msg' => '用户未登录');
        } else {
            $admin['status'] = $status->status;
            if ($admin['status'] <= -1) //超管
            {
                if ($id != -1) { //条件筛选
                    $findInfo = Post::where('status', $id)->get()->toArray();
                } else {
                    $findInfo = Post::get()->toArray();
                }
                // dd($findInfo);
                for ($i = 0; $i < count($findInfo); $i++) {
                    $findUser = User::withTrashed()->select('user_name')->where('id', $findInfo[$i]['user_id'])->get()->toArray();
                    // var_dump($findUser);
                    if ($findInfo[$i]['user_id'] == 0) //用户已删除
                        $findUser[0] = ['user_name' => '已注销的用户'];
                    $findColumn = Column::select('column_name')->where('id', $findInfo[$i]['column_id'])->get()->toArray();
                    // var_dump($findUser[0]['user_name']);
                    $data[$i] = array(
                        'id' => $findInfo[$i]['id'],
                        'auth' => $findUser[0]['user_name'],
                        'auth_id' => $findInfo[$i]['user_id'],
                        'title' => $findInfo[$i]['title'],
                        'column' => $findColumn[0]['column_name'],
                        'created_at' => $findInfo[$i]['created_at'],
                        'status' => $findInfo[$i]['status']
                    );
                }
                $item = array_slice($data, ($current_page - 1) * $perPage, $perPage); //$Array为要分页的数组
                $totals = count($data);
                $paginator = new LengthAwarePaginator($item, $totals, $perPage, $current_page, [
                    'path' => Paginator::resolveCurrentPath(),
                    'pageName' => 'page',
                ]);
                $res = array('code' => 200, 'msg' => 'success', 'data' => $paginator);
            }
            if ($admin['status'] == 0) //管理员
            {
                $x = 0;
                $group = User::withTrashed()->select('id')->where('status', $status->id)->orWhere('status', -($status->id))->get()->toArray();
                for ($count = 0; $count < count($group); $count++) {
                    if ($id != -1) {
                        $findInfo = Post::where('user_id', $group[$count]['id'])->where('status', $id)->get()->toArray();
                    } else {
                        $findInfo = Post::where('user_id', $group[$count]['id'])->get()->toArray();
                    }
                    for ($i = 0; $i < count($findInfo); $i++) {
                        $findUser = User::withTrashed()->select('user_name')->where('id', $findInfo[$i]['user_id'])->get()->toArray();
                        if ($findInfo[$i]['user_id'] == 0) //用户已删除
                            $findUser[0] = ['user_name' => '已注销的用户'];
                        $findColumn = Column::select('column_name')->where('id', $findInfo[$i]['column_id'])->get()->toArray();
                        $data[$x++] = array(
                            'id' => $findInfo[$i]['id'],
                            'auth' => $findUser[0]['user_name'],
                            'auth_id' => $findInfo[$i]['user_id'],
                            'title' => $findInfo[$i]['title'],
                            'column' => $findColumn[0]['column_name'],
                            'created_at' => $findInfo[$i]['created_at'],
                            'status' => $findInfo[$i]['status'],
                        );
                    }
                }
                $item = array_slice($data, ($current_page - 1) * $perPage, $perPage); //$Array为要分页的数组
                $totals = count($data);
                $paginator = new LengthAwarePaginator($item, $totals, $perPage, $current_page, [
                    'path' => Paginator::resolveCurrentPath(),
                    'pageName' => 'page',
                ]);
                $res = array('code' => 200, 'msg' => 'success', 'data' => $paginator);
            }
            if ($admin['status'] > 0) {
                $res = array('code' => 100, 'msg' => '该用户权限不足');
            }
        }
        // dd($res);
        return response()->json($res);
    }

    /**
     *搜索(待优化)$findInfo= Post::withTrashed()->where('title','like','%'.$text.'%')->get();
     */
    public function getSearchArticle(Request $request)
    {
        $text = $request->input('keyword');
        // dd($text);
        $res = [];
        $data = [];
        $status = User::find(Auth::id());
        // $status = User::withTrashed()->find(5); //测试使用withTrashed()
        $perPage = 10;            //每页显示数量
        if ($request->has('page')) {
            $current_page = $request->input('page');
            $current_page = $current_page <= 0 ? 1 : $current_page;
        } else {
            $current_page = 1;
        }
        if ($status == null) {
            $res = array('code' => 100, 'msg' => '用户未登录');
        } else {
            $admin['status'] = $status->status;
            if ($admin['status'] <= -1) //超管
            {
                $findInfo = Post::where('title', 'like', '%' . $text . '%')->get()->toArray();
                // dd($findInfo);
                for ($i = 0; $i < count($findInfo); $i++) {
                    $findUser = User::withTrashed()->select('user_name')->where('id', $findInfo[$i]['user_id'])->get()->toArray();
                    if ($findInfo[$i]['user_id'] == 0) //用户已删除
                        $findUser[0] = ['user_name' => '已注销的用户'];
                    $findColumn = Column::select('column_name')->where('id', $findInfo[$i]['column_id'])->get()->toArray();
                    $data[$i] = array(
                        'id' => $findInfo[$i]['id'],
                        'auth' => $findUser[0]['user_name'],
                        'auth_id' => $findInfo[$i]['user_id'],
                        'title' => $findInfo[$i]['title'],
                        'column' => $findColumn[0]['column_name'],
                        'created_at' => $findInfo[$i]['created_at'],
                        'status' => $findInfo[$i]['status'],
                    );
                }
                $item = array_slice($data, ($current_page - 1) * $perPage, $perPage); //$Array为要分页的数组
                $totals = count($data);
                $paginator = new LengthAwarePaginator($item, $totals, $perPage, $current_page, [
                    'path' => Paginator::resolveCurrentPath(),
                    'pageName' => 'page',
                ]);
                $res = array('code' => 200, 'msg' => 'success', 'data' => $paginator);
            }
            if ($admin['status'] == 0) //管理员
            {
                $x = 0;
                $group = User::withTrashed()->select('id')->where('status', $status->id)->orWhere('status', -($status->id))->get()->toArray();
                for ($count = 0; $count < count($group); $count++) {
                    $findInfo = Post::where('user_id', $group[$count]['id'])->where('title', 'like', '%' . $text . '%')->get()->toArray();
                    for ($i = 0; $i < count($findInfo); $i++) {
                        $findUser = User::withTrashed()->select('user_name')->where('id', $findInfo[$i]['user_id'])->get()->toArray();
                        if ($findInfo[$i]['user_id'] == 0) //用户已删除
                            $findUser[0] = ['user_name' => '已注销的用户'];
                        $findColumn = Column::select('column_name')->where('id', $findInfo[$i]['column_id'])->get()->toArray();
                        $data[$x++] = array(
                            'id' => $findInfo[$i]['id'],
                            'auth' => $findUser[0]['user_name'],
                            'auth_id' => $findInfo[$i]['user_id'],
                            'title' => $findInfo[$i]['title'],
                            'column' => $findColumn[0]['column_name'],
                            'created_at' => $findInfo[$i]['created_at'],
                            'status' => $findInfo[$i]['status'],
                        );
                    }
                }
                $item = array_slice($data, ($current_page - 1) * $perPage, $perPage); //$Array为要分页的数组
                $totals = count($data);
                $paginator = new LengthAwarePaginator($item, $totals, $perPage, $current_page, [
                    'path' => Paginator::resolveCurrentPath(),
                    'pageName' => 'page',
                ]);
                $res = array('code' => 200, 'msg' => 'success', 'data' => $paginator);
            }
            if ($admin['status'] > 0) {
                $res = array('code' => 100, 'msg' => '该用户权限不足');
            }
        }
        return response()->json($res);
    }

    /**
     *
     */
    public function getsoftDeleteInfo(Request $request)
    {
        $res = [];
        $data = [];
        $status = User::find(Auth::id());
        //$status=User::withTrashed()->find(4);//测试使用
        $perPage = 10;            //每页显示数量
        if ($request->has('page')) {
            $current_page = $request->input('page');
            $current_page = $current_page <= 0 ? 1 : $current_page;
        } else {
            $current_page = 1;
        }
        if ($status == null) {
            $res = array('code' => 100, 'msg' => '用户未登录');
        } else {
            $admin['status'] = $status->status;
            if ($admin['status'] <= -1) //超管
            {
                $findInfo = Post::onlyTrashed()->get()->toArray();
                for ($i = 0; $i < count($findInfo); $i++) {
                    $findUser = User::withTrashed()->select('user_name')->where('id', $findInfo[$i]['user_id'])->get()->toArray();
                    if ($findInfo[$i]['user_id'] == 0) //用户已删除
                        $findUser[0] = ['user_name' => '已注销的用户'];
                    $findColumn = Column::select('column_name')->where('id', $findInfo[$i]['column_id'])->get()->toArray();
                    $data[$i] = array(
                        'id' => $findInfo[$i]['id'],
                        'auth' => $findUser[0]['user_name'],
                        'auth_id' => $findInfo[$i]['user_id'],
                        'title' => $findInfo[$i]['title'],
                        'column' => $findColumn[0]['column_name'],
                        'created_at' => $findInfo[$i]['created_at'],
                        'status' => $findInfo[$i]['status'],
                    );
                }
                $item = array_slice($data, ($current_page - 1) * $perPage, $perPage); //$Array为要分页的数组
                $totals = count($data);
                $paginator = new LengthAwarePaginator($item, $totals, $perPage, $current_page, [
                    'path' => Paginator::resolveCurrentPath(),
                    'pageName' => 'page',
                ]);
                $res = array('code' => 200, 'msg' => 'success', 'data' => $paginator);
            }
            if ($admin['status'] == 0) //管理员
            {
                $x = 0;
                $group = User::withTrashed()->select('id')->where('status', $status->id)->orWhere('status', -($status->id))->get()->toArray();
                for ($count = 0; $count < count($group); $count++) {
                    $findInfo = Post::onlyTrashed()->where('user_id', $group[$count]['id'])->get()->toArray();
                    for ($i = 0; $i < count($findInfo); $i++) {
                        $findUser = User::withTrashed()->select('user_name')->where('id', $findInfo[$i]['user_id'])->get()->toArray();
                        if ($findInfo[$i]['user_id'] == 0) //用户已删除
                            $findUser[0] = ['user_name' => '已注销的用户'];
                        $findColumn = Column::select('column_name')->where('id', $findInfo[$i]['column_id'])->get()->toArray();
                        $data[$x++] = array(
                            'id' => $findInfo[$i]['id'],
                            'auth' => $findUser[0]['user_name'],
                            'auth_id' => $findInfo[$i]['user_id'],
                            'title' => $findInfo[$i]['title'],
                            'column' => $findColumn[0]['column_name'],
                            'created_at' => $findInfo[$i]['created_at'],
                            'status' => $findInfo[$i]['status'],
                        );
                    }
                }
                $item = array_slice($data, ($current_page - 1) * $perPage, $perPage); //$Array为要分页的数组
                $totals = count($data);
                $paginator = new LengthAwarePaginator($item, $totals, $perPage, $current_page, [
                    'path' => Paginator::resolveCurrentPath(),
                    'pageName' => 'page',
                ]);
                $res = array('code' => 200, 'msg' => 'success', 'data' => $paginator);
            }
            if ($admin['status'] > 0) {
                $res = array('code' => 100, 'msg' => '该用户权限不足');
            }
        }
        return response()->json($res);
    }

    /**
     * 软删除文章
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function postSoftDelete(Article $request)
    {
        $res = array();
        $id = $request->input('number');
        $find = Post::find($id);
        if ($find != null) {
            $res = array('code' => 200, 'msg' => '软删除' . $find->title);
            $find->delete();
        } else {
            $res = array('code' => 100, 'msg' => '该文章已软删除或不存在');
        }
        return response()->json($res);
    }

    /**
     * 彻底删除文章
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */

    public function postDelete(Article $request)
    {
        $res = array();
        $id = $request->input('number');
        $find = Post::onlyTrashed()->find($id);
        if ($find != null) {
            $find->forceDelete();
            $res = array('code' => 200, 'msg' => '彻底删除' . $find->title);
            // $this->sendInfo('您的文章"' . $find->title . '"已被彻底删除,有关信息请询问管理员', $find->user_id, $find->id, 1);
        } else {
            $res = array('code' => 100, 'msg' => '失败:该文章未被软删除或文章不存在');
        }
        return response()->json($res);
    }

    /**
     * 恢复软删除文章
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */

    public function postRecovr(Article $request)
    {
        $res = array();
        $id = $request->input('number');
        $find = Post::withTrashed()->find($id);
        if ($find != null) //是否存在
        {
            if ($find->deleted_at != null) //是否是软删除
            {
                $find->restore();
                $res = ['code' => 200, 'msg' => '恢复软删除' . $find->title];
            } else {
                $res = array('code' => 100, 'msg' => '失败：文章为正常状态');
            }
        } else {
            $res = array('code' => 100, 'msg' => '失败：找不到该文章或该文章已不存，');
        }
        return response()->json($res);
    }

    //    /**
    //     * 审核文章
    //     * @param Request $request
    //     */
    //    public function getArticle(Article $request){
    //        $id=$request->input('number');
    //        $find=Post::where('id',$id)->get()->toArray();
    //
    //
    //    }


    /**
     * 通过审核
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */

    public function postPass(Article $request)
    {
        $res = array();
        $id = $request->input('number');
        $find = Post::withTrashed()->find($id);
        if ($find != null) {
            $find->status = 1;
            $find->save();
            $res = array('code' => 200, 'msg' => '通过' . $find->title);
            $this->sendInfo('您的文章' . $find->title . '已通过审核', $find->user_id, $find->id, 1);
        } else {
            $res = array('code' => 100, 'msg' => '该文章不存在');
        }
        return response()->json($res);
    }

    /**
     * 未通过审核
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function postNotPass(Article $request)
    {
        $res = array();
        $id = $request->input('number');
        $find = Post::withTrashed()->find($id);
        if ($find != null) {
            $find->status = 0;
            $find->save();
            $res = array('code' => 200, 'msg' => '审核未通过' . $find->title);
            $this->sendInfo('您的文章' . $find->title . '未通过审核', $find->user_id, $find->id, 1);
        } else {
            $res = array('code' => 100, 'msg' => '该文章不存在');
        }
        return response()->json($res);
    }

    //增添信息
    public function sendInfo($msg, $pull_id, $post_id, $push_id)
    {
        $find = Message::insert(['pull_id' => $pull_id, 'post_id' => $post_id, 'push_id' => $push_id, 'status' => 0, 'read' => 0, 'feedback' => $msg]);
    }
}
